package com.lxk.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxk.pojo.Ticket;
import com.lxk.pojo.UserTicket;
import com.lxk.service.TicketService;
import com.lxk.service.UserTicketService;
import com.lxk.util.CalendarUtil;
import com.lxk.vo.PageModel;
import com.lxk.vo.TicketCountVO;
import com.lxk.vo.result.CodeMsg;
import com.lxk.vo.result.Result;
import org.omg.CORBA.CODESET_INCOMPATIBLE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>
 * 现金券模板 前端控制器
 * </p>
 *
 * @author JohnSoybean
 * @since 2020-07-14
 */
@RestController
@RequestMapping("/ticket")
public class TicketController {

    @Autowired
    private UserTicketService userTicketService;

    /**
     * 实体店消费券统计数量
     *
     * @return
     */
    @GetMapping("/count")
    public Result<TicketCountVO> showCount(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        QueryWrapper<UserTicket> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("user_id", userId);
        wrapper1.eq("progress", 1);
        int count1 = userTicketService.count(wrapper1);
        QueryWrapper<UserTicket> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("user_id", userId);
        wrapper2.eq("progress", 0);
        wrapper2.lt("end_time", CalendarUtil.getTodayZero());
        int count2 = userTicketService.count(wrapper2);
        QueryWrapper<UserTicket> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("user_id", userId);
        wrapper3.eq("progress", 0);
        wrapper3.ge("end_time", CalendarUtil.getTodayZero());
        int count3 = userTicketService.count(wrapper3);
        TicketCountVO vo = new TicketCountVO(count3, count1, count2);
        return Result.success(vo);
    }

    /**
     * 实体店消费券列表
     *
     * @return
     */
    @GetMapping("/list")
    public Result<PageModel<UserTicket>> showUserTicketList(HttpServletRequest request, Integer pageNum, Integer pageSize, Integer progress) {
        Long userId = (Long) request.getAttribute("userId");
        Page<UserTicket> page = new Page<>(pageNum, pageSize);
        QueryWrapper<UserTicket> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("user_ticket_id");
        wrapper.eq("user_id", userId);
        if (progress == 1) {    //  已使用
            wrapper.eq("progress", 1);
        } else if (progress == 2) { //已失效
            wrapper.eq("progress", 0);
            wrapper.lt("end_time", CalendarUtil.getTodayZero());
        } else {    //未使用
            wrapper.eq("progress", 0);
            wrapper.ge("end_time", CalendarUtil.getTodayZero());
        }
        userTicketService.page(page, wrapper);
        PageModel<UserTicket> model = new PageModel<>(page);
        return Result.success(model);
    }

    /**
     * 核销实体店消费券
     *
     * @return
     */
    @GetMapping("/use")
    public Result<Integer> userUserTicket(HttpServletRequest request, Long userTicketId) {
        Long userId = (Long) request.getAttribute("userId");
        UserTicket ticket = userTicketService.getById(userTicketId);
        if (ticket == null) {
            return Result.error(CodeMsg.TICKET_NOT_EXIST);
        }
        //判断是否为该用户、是否已使用、是否过期
        if (!ticket.getUserId().equals(userId)) {
            return Result.error(CodeMsg.TICKET_USER_ERR);
        }
        if (ticket.getProgress() != 0) {
            return Result.error(CodeMsg.TICKET_USED);
        }
        Date endTime = ticket.getEndTime();
        Date todayZero = CalendarUtil.getTodayZero();
        if (endTime.before(todayZero)) {
            return Result.error(CodeMsg.TICKET_EXPIRED);
        }
        //使用
        UserTicket updTicket = new UserTicket();
        updTicket.setUserTicketId(userTicketId);
        updTicket.setProgress(1);
        boolean flag = userTicketService.updateById(updTicket);
        if (flag) {
            return Result.success(null);
        } else {
            return Result.error(CodeMsg.TICKET_USE_ERR);
        }
    }


}

